﻿using Bilgesoft.Framework.Server.ExceptionManagement;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bilgesoft.Framework.Server.DatabaseLayer
{
    public class DatabaseContext : IDisposable
    {

        private CustomContext _context;
        private static DatabaseContext sm_context;
        public static DatabaseContext CreateContext(string connection)
        {
            if (sm_context == null)
                sm_context = new DatabaseContext(connection);
            return sm_context;
        }


        private DatabaseContext(string connection)
        {
            _context = CustomContext.CreateContext(connection);
            _context.InitializeSettings();
        }

        public int SaveChanges()
        {
            try
            {
                ///
                return _context.SaveChanges();
            }
            catch (SqlException ex)
            {
                throw new DBException(ex.Message);
            }
            catch (Exception ex1)
            {
                throw ex1;
            }
                 
        }

        public DbSet<TEntity> Set<TEntity>() where TEntity : class
        {
            ///
            return _context.Set<TEntity>();
        }
        public void Dispose()
        {
            _context.Dispose();
        }
    }
}
